<script setup>
	import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
	import {
		onLoad,
		onShow,
		onUnload
	} from '@dcloudio/uni-app'
	import {
		isObjectValuesNotEmpty
	} from '@/utils/objIsnull.js'
	import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
	import DescriptionAttachment from '@/components/audit/function/descriptionAttachment.vue'
	import {
		ref
	} from 'vue'
	import dayjs from 'dayjs'
	import optionDeptPerson from '@/store/modules/optionDeptPerson'
	import {
		getUserProfile
	} from '@/api/system/user'
	import {
		addPersonnelApply
	} from '@/api/attendance'
	import approvalProcessState from '@/store/modules/approvalProcess'

	const title = '转正审批'
	const fileSubtitleValue = '需提交人上传：1.转正申请表 2.转正人员绩效考核模板（由人事部提供电子版模板）'
	const useDateRef = ref(null)
	const returnDateRef = ref(null)
	const endTimeMax = ref('') // 最大转正日期
	const ItemRequisitionList = ref({
		date: '',
		date2: '',
		money: '',
		deptName: '',
		deptId: '',
		remark: '',
		escorts: [],
	})


	const escortsFn = () => {
		uni.navigateTo({
			url: '/components/OptionDeptPerson/index?type=person&isDanXuan=1',
		})
	}


    //时间选择功能
	const formatter = (type, value) => {
		if (type === 'year') {
			return `${value}年`
		}
		if (type === 'month') {
			return `${value}月`
		}
		if (type === 'day') {
			return `${value}日`
		}
		if (type === 'hour') {
			return `${value}时`
		}
		if (type === 'minute') {
			return `${value}分`
		}
		return value
	}
	//入职日期
	let timeChuo = ref(0)
	let useDatePicker = ref(JSON.stringify(dayjs()))
	const useDateFn = () => {
		useDatePicker.value = Date.now(); // 打开选择器显示当前最新时间
		useDateRef.value.open()
	}
	const useDateConfirm = (obj) => {
		ItemRequisitionList.value.date = dayjs(obj.value).format('YYYY-MM-DD')
		let newTime= new Date(ItemRequisitionList.value.date)
		timeChuo.value = newTime.getTime()
		if(timeChuo.value>=timeChuo2.value && timeChuo2.value!='' && timeChuo.value!=''){
			ItemRequisitionList.value.date=''
			timeChuo.value= 0
			uni.showToast({
				title: '开始时间不能大于等于结束时间',
				duration: 2000,
				icon: 'none',
			})
			return false
		}
	}

	const returnDateFn = () => {
		if (ItemRequisitionList.value.date) {
			returnDatePicker.value = Date.now(); // 打开选择器显示当前最新时间
			returnDateRef.value.open()
		} else {
			uni.showToast({
				title: '请先选择入职日期',
				icon: 'none'
			})
		}
	}
	//转正日期
	let timeChuo2 = ref(0)
	let returnDatePicker = ref(JSON.stringify(dayjs()))
	const returnDateConfirm = (obj) => {
		ItemRequisitionList.value.date2 = dayjs(obj.value).format('YYYY-MM-DD')
		let newTime= new Date(ItemRequisitionList.value.date2)
		timeChuo2.value = newTime.getTime()
		if(timeChuo2.value<=timeChuo.value && timeChuo.value!=''){
			ItemRequisitionList.value.date2=''
			timeChuo2.value = 0
			uni.showToast({
				title: '结束时间不能小于等于开始时间',
				duration: 2000,
				icon: 'none',
			})
			return false
		}
	}

	const usersFn = () => {
		uni.navigateTo({
			url: '/components/OptionDeptPerson/index?type=person',
		})
	}
	const withSealFn = () => {
		uni.navigateTo({
			url: '/components/OptionDeptPerson/index',
		})
	}

	const getUserProfileFn = async () => {
		let {
			code,
			data
		} = await getUserProfile()
		if (code === 200) {
			userId.value = data.userId
			// ItemRequisitionList.value.users = data.nickName
		}
	}
	const goHistoryView = () => {
		uni.navigateTo({
			url: '/components/TypeDefineModel?type=转正审批&apiName=人事',
		})
	}
	let attchment = ref([])
	const getDesignDrawingsEmit = (emit) => {
		attchment.value = emit
	}
	const userId = ref(null)
	const clickSave = async () => {
		ItemRequisitionList.value.attchment = attchment.value
		// console.log(ItemRequisitionList.value,'ItemRequisitionList.value')
		if (isObjectValuesNotEmpty(ItemRequisitionList.value)) {
			let params = {
				date: dayjs(ItemRequisitionList.value.date).format('YYYY-MM-DD hh:mm:ss'),
				lastDate: dayjs(ItemRequisitionList.value.date2).format('YYYY-MM-DD hh:mm:ss'),
				details: ItemRequisitionList.value.money,
				userId: userId.value,
				deptId: ItemRequisitionList.value.deptId,
				deptName: ItemRequisitionList.value.deptName,
				trialPost: ItemRequisitionList.value.remark,
				personnel: JSON.stringify(ItemRequisitionList.value.escorts),
				ruleId: 'zhuanzheng',
				type: '转正审批',
				attchment: JSON.stringify(attchment.value),
				approve: approvalProcessState.state.approvalProcess,
			}
			console.log(params, '转正审批提交数据params')
			let {
				code
			} = await addPersonnelApply(params)
			if (code === 200) {
				uni.showToast({
					title: '提交成功',
					duration: 1500,
					icon: 'success',
				})

				setTimeout(() => {
					uni.navigateBack()
				}, 1000)
			}
		} else {
			uni.showToast({
				title: '请输入完整数据',
				icon: 'error'
			})
		}
	}

	onLoad(() => {
		getUserProfileFn()
	})
	onShow(() => {
		// if (optionDeptPerson.state.optionPerson) {
		// 	ItemRequisitionList.value.escorts = optionDeptPerson.state.optionPerson.map((v) => {
		// 		return {
		// 			userId: v.userId,
		// 			nickName: v.nickName,
		// 		}
		// 	})
		// }
		// if (optionDeptPerson.state.optionDept) {
		// 	ItemRequisitionList.value.deptName = optionDeptPerson.state.optionDept.label
		// 	ItemRequisitionList.value.deptId = optionDeptPerson.state.optionDept.depId
		// }
		// 监听事件 获取选择的人员
		uni.$on('getOptionPerson', (data) => {
			ItemRequisitionList.value.escorts = data.map((v) => {
				return {
					userId: v.userId,
					nickName: v.nickName,
				}
			})
		});

		// 监听事件 获取选择的部门
		uni.$on('getOptionDept', (data) => {
			// console.log(data, '部门data')
			ItemRequisitionList.value.deptName = optionDeptPerson.state.optionDept.label
			ItemRequisitionList.value.deptId = optionDeptPerson.state.optionDept.depId
		});
	})

	onUnload(() => {
		uni.$off('getOptionPerson');
	})
</script>

<template>
	<view>
		<!--   转正审批  -->
		<HeaderNavTabarSvg :title="title">
			<image class="bukaSvg" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView"></image>
		</HeaderNavTabarSvg>

		<view class="picker-content" @tap="escortsFn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">转正员工姓名</text>
				<view class="info-list-right">
					<view v-if="ItemRequisitionList.escorts.length > 0" v-for="(item,index) in ItemRequisitionList.escorts"
						:key="index" class="uni-input">{{ item.nickName }}
						&nbsp
					</view>
					<view v-else class="uni-inputs">请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>

		<view class="picker-content" @tap="withSealFn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">所在部门</text>
				<view class="info-list-right">
					<view v-if="ItemRequisitionList.deptName" class="uni-input">{{ ItemRequisitionList.deptName }}
					</view>
					<view v-else class="uni-inputs">请选择</view>

					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>

		<view class="picker-content" @tap="useDateFn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">入职日期</text>
				<view class="info-list-right">
					<view v-if="ItemRequisitionList.date" class="uni-input">{{ ItemRequisitionList.date }}
					</view>
					<view v-else class="uni-inputs">请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>
		<uv-datetime-picker ref="useDateRef" v-model="useDatePicker" :formatter="formatter" mode="date"
			@confirm="useDateConfirm" />

		<view class="picker-content" @tap="returnDateFn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">转正日期</text>
				<view class="info-list-right">
					<view class="uni-input" v-if="ItemRequisitionList.date2">{{ ItemRequisitionList.date2 }}
					</view>
					<view class="uni-inputs" v-else>请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>
		<uv-datetime-picker ref="returnDateRef" :minDate="timeChuo"  v-model="returnDatePicker"
			:formatter="formatter" mode="date" @confirm="returnDateConfirm">
		</uv-datetime-picker>

		<view class="textarea-box">
			<text class="info-list-left isrequired">转正后定岗职级</text>
			<textarea v-model="ItemRequisitionList.remark" class="textinfo-list" placeholder="请输入"></textarea>
		</view>

		<view class="textarea-box">
			<text class="info-list-left isrequired">转正后薪资总额及薪资结构</text>
			<textarea v-model="ItemRequisitionList.money" class="textinfo-list" placeholder="请输入"></textarea>
		</view>

		<!--    说明附件-->
		<DescriptionAttachment @getDesignDrawingsEmit="getDesignDrawingsEmit" :isrequired="true"
			:fileSubtitle="fileSubtitleValue" />
		<!--    审批流程-->
		<AuditApplicationSteps :ruleId="'zhuanzheng'" />
		<!--    底部占位符-->
		<view class="blank"></view>
		<!--    保存-->
		<view class="buttonContainer">
			<button class="buttonClass" @click="clickSave">提交</button>
		</view>
	</view>
</template>

<style lang="scss" scoped>
	@import '@/static/style/index.scss';

	.bukaSvg {
		width: 32rpx;
		height: 32rpx;
	}

	.buttonContainer {
		position: fixed;
		bottom: 0;
		display: flex;
		justify-content: center;
		width: 100%;
		align-items: center;
		padding: 16rpx;
		background-color: #fff;
	}

	.buttonClass {
		display: flex;
		width: 686rpx;
		height: 72rpx;
		justify-content: center;
		align-items: center;
		background: #165dff;
		border-radius: 16rpx;
		color: #fff;
		font-size: 32rpx;
		//margin-top: 40rpx;
		overflow: hidden;
	}

	.uni-inputs {
		color: #86909c !important;
	}

	.uni-input {
		text-align: right;
		font-size: 28rpx;
	}

	.blank {
		height: calc(72rpx + 32rpx + 20rpx);
	}

	.textarea-box {
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.textinfo-list {
			margin-top: 20rpx;
			width: 100%;
			height: 200rpx;
		}
	}

	.bukabanci {
		.info-content {
			color: #86909c !important;
		}

		.info-list-left {
			// @include font-color-auxiliary;
			color: #86909c !important;
		}
	}

	.textarea-box {
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.textinfo-list {
			margin-top: 20rpx;
			width: 100%;
			height: 200rpx;
		}
	}

	.popup-content {
		background: #f7f8fa;

		.header-box {
			padding: 30rpx 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			p {
				color: #1d2129;
				font-size: 34rpx;
				font-weight: 500;
			}

			.module-image-ba {
				border-radius: 32rpx;
				background: #f2f3f5;
				width: 45rpx;
				height: 45rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				image {
					width: 32rpx;
					height: 32rpx;
				}
			}
		}

		.card-list {
			display: flex;
			align-items: center;
			flex-direction: column;
			padding: 20rpx 30rpx;
			box-sizing: border-box;

			.list {
				box-shadow: 0 4rpx 16rpx 0 #0000000d;
				box-sizing: border-box;
				width: 100%;
				background: #fff;
				padding: 16rpx 24rpx;
				border-radius: 8rpx;
				margin-bottom: 10rpx;

				h4 {
					color: #1d2129;
					font-size: 32rpx;
					font-weight: 400;
				}

				p {
					color: #86909c;
					font-size: 24rpx;
					font-weight: 400;
					margin-top: 10rpx;
				}
			}
		}
	}

	.detail-itemRequisition {
		.detail-item {
			padding: 10rpx 30rpx;
			@include flex($align: center, $justify: space-between);

			.detail-utils {
				@include flex($align: center, $justify: space-between);
				width: 150rpx;
				color: #165dff;
			}
		}

		.text-info {
			color: #86909c;
			padding: 20rpx 30rpx;
			display: inline-block;
		}
	}

	.measurement-info-list {
		@include flex($align: center, $justify: space-between);
		padding: $spacing-row-base;
		background: #fff;
		margin-top: 16rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.info-list-right {
			@include flex($align: center);

			.info-content {
				@include textStyleBase;
				font-size: 34rpx;
			}

			.iconfont {
				margin-left: 16rpx;
				// @include font-color-auxiliary;
				color: $uni-fc-fu-lin;
			}
		}

		.unit {
			// @include font-color-auxiliary;
			color: $uni-fc-fu-lin;
		}
	}
</style>